package com.skzh.drawtask.service.impl;

import com.skzh.common.core.domain.model.LoginUser;
import com.skzh.common.utils.DateUtils;
import com.skzh.common.utils.SecurityUtils;
import com.skzh.drawtask.domain.TaskSurface;
import com.skzh.drawtask.domain.TaskSurfacePoints;
import com.skzh.drawtask.mapper.SurfaceMapper;
import com.skzh.drawtask.service.ISurfaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author zr
 * @Description
 * @Date 2025/3/31 16:13
 */
@Service
public class SurfaceServiceImpl implements ISurfaceService {

    @Autowired
    private SurfaceMapper surfaceMapper;

    /**
     * 新增面任务
     */
    @Override
    @Transactional
    public int insertTaskSurface(TaskSurface taskSurface) {

        //1.插入面任务主体
        TaskSurface taskSurfaceMain = new TaskSurface();
        taskSurfaceMain.setSurfaceArea(taskSurface.getSurfaceArea());
        taskSurfaceMain.setSurfaceName(taskSurface.getSurfaceName());
        taskSurfaceMain.setSurfaceColor(taskSurface.getSurfaceColor());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String username = loginUser.getUsername();
        taskSurfaceMain.setCreateBy(username);
        taskSurfaceMain.setCreateTime(DateUtils.getNowDate());

        //2.插入并获取生成的主键ID
        surfaceMapper.insertTaskSurface(taskSurfaceMain);
        Long surfaceId = taskSurfaceMain.getId();

        //3.批量插入面顶点数据
        if (taskSurface.getPoints() != null && !taskSurface.getPoints().isEmpty()) {
            List<TaskSurfacePoints> TaskSurfacePointsList = taskSurface.getPoints().stream()
                    .map(SurfacePoints -> {
                        TaskSurfacePoints taskSurfacePoints = new TaskSurfacePoints();
                        taskSurfacePoints.setSurfaceId(surfaceId);
                        taskSurfacePoints.setMLongitude(SurfacePoints.getMLongitude());
                        taskSurfacePoints.setMLatitude(SurfacePoints.getMLatitude());
                        taskSurfacePoints.setMOrder(SurfacePoints.getMOrder());
                        taskSurfacePoints.setCreateBy(username);
                        taskSurfacePoints.setCreateTime(DateUtils.getNowDate());
                        return taskSurfacePoints;
                    }).collect(Collectors.toList());
            surfaceMapper.batchInsertTaskLinePoints(TaskSurfacePointsList);
        }

        return 1;
    }

    /**
     * 根据ID删除面任务
     * 根据id批量删除面对应的顶点信息
     */
    @Override
    @Transactional
    public int deleteTaskSurfaceById(Long id) {
        if (id != null) {
            //根据ID批量删除面对应的顶点信息
            surfaceMapper.deleteTaskSurfacePointsBySurfaceId(id);
            surfaceMapper.deleteTaskSurfaceById(id);
        }
        return 1;
    }

    /**
     * 查询面任务
     */
    @Override
    public List<TaskSurface> selectTaskSurfaceList(TaskSurface taskSurface) {
        return surfaceMapper.selectTaskSurfaceList(taskSurface);
    }

    /**
     * 根据ID批量查询对应面的顶点信息
     */
    @Override
    public List<TaskSurfacePoints> selectTaskSurfacePointsByIds(List<Long> surfaceIds) {
        return surfaceMapper.selectTaskSurfacePointsByIds(surfaceIds);
    }

    /**
     * 根据ID批量删除面任务
     *
     * */
    @Override
    @Transactional
    public int deleteTaskSurfaceByIds(List<Long> ids) {
        if (!ids.isEmpty()) {
            //根据ID批量删除面任务
            surfaceMapper.deleteTaskSurfaceByIds(ids);
            //根据ID批量删除面对应的顶点信息
            ids.forEach(id -> surfaceMapper.deleteTaskSurfacePointsBySurfaceId(id));
        }
        return 1;
    }
}
